﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using com.iss.inventory;
using com.iss.inventory.Entity;
using com.iss.inventory.CustomModels;
using com.iss.inventory.Controllers;
using System.Web.UI.HtmlControls;


namespace Staionery_Inventory
{
    public partial class StationeryRequisitionForm : System.Web.UI.Page
    {
        MainController mc;
        StationeryRequestController src;
        RequestListModel requestListModel;
        InvManagementEntities inv;
    
        List<StationeryItem> itemList=new List<StationeryItem>();
   

        protected void Page_Load(object sender, EventArgs e)
        {
            //for web address security
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }


            requestListModel = new RequestListModel();
            mc = new MainController();
            inv = new InvManagementEntities();
            src=new StationeryRequestController();

            //assign user Name
            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }
            string role=null;
            if (Session["role"] != null)
            {
                role = (string)Session["role"];
            }
            //assign departmentName
            lblDepartmentName.Text = (string)(Session["Department"]);

            if (Request.QueryString["Status"] != null)
            {
                
                txtSpecialRequest.Enabled = false;
                 btnsubmit.OnClientClick = null;
                if (Session["Pending"] != null && !IsPostBack)
                {
                    gvStationeryRequisition.DataSource = Session["Pending"] as List<object>;
                    gvStationeryRequisition.DataBind();
                    
                }

                else
                {
                    btnsubmit.Enabled = false;
                    btnAddmore.Enabled = false;
                }

                btnCancel.Visible = true;
                btnAddmore.Text = "Reject";
                btnsubmit.Text = "Approve";
                if (Request.QueryString["Status"] != "Pending")
                {
                    txtSpecialRequest.Enabled = false;
                    gvStationeryRequisition.Enabled = false;
                    btnsubmit.Enabled = false;
                    btnAddmore.Enabled = false;
                }
                if (Session["Current_Item"] != null)
                {
                    itemList = Session["Current_Item"] as List<StationeryItem>;
                    gvStationeryRequisition.DataSource = itemList;
                    gvStationeryRequisition.DataBind();
                    Session["Current_Item"] = null;
                }
                if (gvStationeryRequisition.Rows.Count > 0)
                {
                    gvStationeryRequisition.HeaderRow.Cells[4].Visible = false;
                    foreach (GridViewRow r in gvStationeryRequisition.Rows)
                    {
                        r.Cells[4].Visible = false;
                    }
                }
                if (role == "Employee")
                {
                    btnAddmore.Visible = false;
                    btnsubmit.Visible = false;
                    btnCancel.Visible = true;
                }
            }

            else
            {
                if (Session["Search_allselecteddata"] != null)
                {
                    if (Session["Current_Item"] != null)
                    {
                        itemList = Session["Current_Item"] as List<StationeryItem>;
                        if (Session["Search_allselecteddata"] != null)
                        {
                            //Add Code
                            List<StationeryItem> beforecheck = new List<StationeryItem>();
                            List<StationeryItem> aftercheck = new List<StationeryItem>();
                            beforecheck = Session["Search_allselecteddata"] as List<StationeryItem>;
                            foreach (StationeryItem si in beforecheck)
                            {
                                int i = 0;
                                foreach (StationeryItem csi in itemList)
                                {
                                    if (si.Description == csi.Description)
                                    {
                                        i++;
                                    }
                                }
                                if (i == 0)
                                    aftercheck.Add(si);

                            }
                            itemList.AddRange(aftercheck);
                            Session["Search_allselecteddata"] = null;
                        }

                        gvStationeryRequisition.DataSource = itemList;
                        gvStationeryRequisition.DataBind();
                        Session["fordelete"] = itemList;

                    }
                    else if (!IsPostBack)
                    {
                        itemList = Session["Search_allselecteddata"] as List<StationeryItem>;
                        gvStationeryRequisition.DataSource = itemList;
                        gvStationeryRequisition.DataBind();
                        Session["fordelete"] = itemList;
                        Session["Current_Item"] = itemList;
                        Session["Search_allselecteddata"] = null;
                    }


                }

            }
        }

        protected void gvStationeryRequisition_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (btnsubmit.Text == "Submit")
            {
                itemList = Session["fordelete"] as List<StationeryItem>;
                itemList.RemoveAt(e.RowIndex);
                gvStationeryRequisition.DataSource = itemList;
                gvStationeryRequisition.DataBind();
            }
            //else
            //{
            //    List<object> list = Session["Pending"] as List<object>;
            //    list.RemoveAt(e.RowIndex);
            //    int stationeryID = int.Parse(gvStationeryRequisition.Rows[e.RowIndex].Cells[0].Text);
            //    int srfID = Convert.ToInt32(Session["RequestListID"]);
            //    src.DeleteRequestData(srfID,stationeryID);
            //    gvStationeryRequisition.DataSource = list;
            //    gvStationeryRequisition.DataBind();
            //    Session["Pending"] = list;


            //}
 
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (btnsubmit.Text == "Submit")
            {
                if (gvStationeryRequisition.Rows.Count < 1)
                {
                    lblErrorText.Text = "No Data and so Please choose one data Item";
                    btnAddmore.Focus();
                }
                else
                {
                    HiddenField hf = this.Master.FindControl("hfconfirm") as HiddenField;
                    if (hf.Value == "true")
                    {
                        requestListModel.EmployeeID = int.Parse(Session["empID"].ToString());
                        requestListModel.ApproveID = int.Parse(Session["ApproveID"].ToString());
                        requestListModel.DateSubmittion = DateTime.Today;
                        if (txtSpecialRequest.Text != "")
                        {
                            requestListModel.SpecialRequest = txtSpecialRequest.Text;
                        }
                        else
                            requestListModel.SpecialRequest = "None";
                        requestListModel.Status = "Pending";
                        int lastrequestID = mc.StationeryRequestController(requestListModel);
                        AddReustListDetail(gvStationeryRequisition, lastrequestID);
                        lblRequestIDText.Text = "Request ID: ";
                        lblRequestID.Text = lastrequestID.ToString();
                        txtSpecialRequest.Enabled = false;
                        btnAddmore.Enabled = false;
                        btnsubmit.Enabled = false;
                        gvStationeryRequisition.Enabled = false;
                    }
                }
            }
            else if (btnsubmit.Text == "Approve")
            {
                if (Session["RequestListID"] != null)
                {
                    int requestListID = int.Parse(Session["RequestListID"].ToString());
                    var update = (from s in inv.RequestLists where s.RequestID == requestListID select s).First(); ;
                    update.Status = "Approved";
                    inv.SaveChanges();
                    var updatedetail = (from s in inv.RequestDetails where s.RequestID == requestListID select s).ToList();
                    int i = 0;
                    foreach (RequestDetail rd in updatedetail)
                    {
                        TextBox quantity = gvStationeryRequisition.Rows[i].FindControl("txtQuatity") as TextBox;
                        rd.QtyRequested = int.Parse(quantity.Text);
                        inv.SaveChanges();
                        i++;
                    }
                   btnsubmit.Enabled = false;
                   btnAddmore.Enabled = false;
                   gvStationeryRequisition.Enabled = false;
                       }
            }

        }

        public void AddReustListDetail(GridView gv,int requestID)
        {
            foreach (GridViewRow gr in gv.Rows)
            {
                TextBox quantity = ((TextBox)gr.FindControl("txtQuatity"));
                
                mc.StationeryRequestDetailController(requestID,gr.Cells[0].Text,int.Parse(quantity.Text));
            }
        }

        protected void btnAddmore_Click(object sender, EventArgs e)
        {
            if (btnAddmore.Text == "Reject")
            {
                if (Session["RequestListID"] != null)
                {
                    int requestListID = int.Parse(Session["RequestListID"].ToString());
                    var update = (from s in inv.RequestLists where s.RequestID == requestListID select s).First(); ;
                    update.Status = "Rejected";
                     inv.SaveChanges();

                     var updatedetail = (from s in inv.RequestDetails where s.RequestID == requestListID select s).ToList();
                    int i=0;
                     foreach (RequestDetail rd in updatedetail)
                     {
                         TextBox quantity=gvStationeryRequisition.Rows[i].FindControl("txtQuatity") as TextBox;
                         rd.QtyRequested = int.Parse(quantity.Text);
                         inv.SaveChanges();
                         i++;
                     }

                    btnsubmit.Enabled = false;
                    btnAddmore.Enabled = false;
                    gvStationeryRequisition.Enabled = false;
                }
            }

            else
            Response.Redirect("~/Presentation/Search/SearchItemsPage.aspx");
        
        }

        protected void txtQuatity_TextChanged1(object sender, EventArgs e)
        {
            List<StationeryItem> change_text=new List<StationeryItem>();
            if (btnsubmit.Text == "Submit")
            {
                change_text = Session["Current_Item"] as List<StationeryItem>;
                TextBox txt = sender as TextBox;
                GridViewRow gr = txt.NamingContainer as GridViewRow;
                String name = gvStationeryRequisition.Rows[gr.RowIndex].Cells[1].Text;
                foreach (StationeryItem si in change_text)
                {
                    if (si.Description == name)
                        si.QtyLeft = int.Parse(txt.Text);
                }
                Session["Current_Item"] = change_text;
            }
            else
            {
                change_text = Session["Pending"] as List<StationeryItem>;
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Presentation/StationeryRequest/StationeryRequestList.aspx");
        }
    }
      
}